package br.ufrgs.inf.sissw.app.models;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import br.ufrgs.inf.sissw.app.PrediosDialog;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class InfItemizedOverlay extends ItemizedOverlay {

	private ArrayList<InfOverlayItem> mOverlays = new ArrayList<InfOverlayItem>();
	private Context context;

	public InfItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));

		this.context = context;
	}

	public void addOverlay(InfOverlayItem item) {
		if (mOverlays == null) {
			mOverlays = new ArrayList<InfOverlayItem>();
		}
		mOverlays.add(item);
		populate();
	}

	@Override
	protected OverlayItem createItem(int arg0) {
		return mOverlays.get(arg0);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);

		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setTextSize(16);
		paint.setFakeBoldText(true);

		for (InfOverlayItem item : mOverlays) {
			Point screenPts = new Point();
			mapView.getProjection().toPixels(item.getPoint(), screenPts);
			canvas.drawText(item.getText(), screenPts.x - 15, screenPts.y - 36,
					paint);
		}
	}

	@Override
	protected boolean onTap(int index) {
		InfOverlayItem item = mOverlays.get(index);
		PrediosDialog dialog = new PrediosDialog(context, item.getText());
		dialog.setTitle(item.getText());
		dialog.show();
		return true;
	}

}
